<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON.stringify() 用于将 JavaScript 值转换为 JSON 字符串。</title>
</head>

<body>
    <p id="demo"></p>
    <script>
        var str = { "name": "菜鸟教程", "site": "http://www.runoob.com" }
        str_pretty1 = JSON.stringify(str)
        document.write("只有一个参数情况：");
        document.write("<br />");
        document.write("<pre>" + str_pretty1 + "</pre>");
        document.write("<br />");
        str_pretty2 = JSON.stringify(str, null, 4) //使用四个空格缩进
        document.write("使用参数情况：");
        document.write("<br />");
        document.write("<pre>" + str_pretty2 + "</pre>"); // pre 用于格式化输出
    </script>
</body>

</html>